package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageBean;
import com.sky.service.SetmealService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
@AllArgsConstructor
public class SetmealServiceImpl implements SetmealService {

    private SetmealMapper setmealMapper;
    private SetmealDishMapper setmealDishMapper;

    /**
     * 添加套餐信息
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void addWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();

        BeanUtils.copyProperties(setmealDTO, setmeal);

        //向菜品表插入1条数据
        setmealMapper.insert(setmeal);

        //获取insert语句生成的主键值
        Long setmealId = setmeal.getId();

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(x -> x.setSetmealId(setmealId));
            //向口味表插入n条数据
            setmealDishMapper.insertBatch(setmealDishes);//后绪步骤实现
        }
    }

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO 分页条件
     * @return
     */
    @Override
    public PageBean pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageBean(page.getTotal(), page.getResult());
    }

    /**
     * 套餐批量删除
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                //当前套餐处于起售中，不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }

            //删除套餐数据
            setmealMapper.deleteById(id);
            //删除套餐关联的菜品信息
            setmealDishMapper.deleteBySetmealId(id);
        }
    }

    /**
     * 根据id查询套餐详情
     *
     * @param id 要查询的套餐id
     * @return 套餐信息
     */
    @Override
    public SetmealVO getByIdWithDish(Long id) {
        //根据id查询菜品数据
        Setmeal setmeal = setmealMapper.getById(id);

        //根据菜品id查询菜品数据
        List<SetmealDish> setmealDishes = setmealDishMapper.getByDishId(id);//后绪步骤实现

        //将查询到的数据封装到VO
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    /**
     * 更新套餐信息
     *
     * @param setmealDTO 套餐信息
     */
    @Override
    public void updateWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //修改菜品表基本信息
        setmealMapper.update(setmeal);

        //删除原有的口味数据
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());

        //重新插入口味数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();//后绪步骤实现
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(sd -> {
                sd.setDishId(setmealDTO.getId());
            });
            //向套餐菜品表插入n条数据
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 套餐起售、停售
     *
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(Integer status, Integer id) {
        setmealMapper.updateStatus(status,id);
    }
}
